﻿Imports System.Data.SqlClient

Public Class productEditor
    Dim cmd As New SqlCommand
    Dim num As Double

    Private Sub productEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        txtcatid.Hide()
        txtid.Hide()
        Dim cmd As New SqlCommand("select CatID from tblcategory where catName=N'" & txtcatname.Text & "'", cn)
        txtcatid.Text = cmd.ExecuteScalar
    End Sub

    Private Sub txtcatname_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtcatname.MouseHover
        cmd.Connection = cn
        cmd.CommandText = "select CatName from tblCategory"
        Dim reader = cmd.ExecuteReader
        Dim dt As New DataTable
        dt.Load(reader)
        reader.Close()
        Me.txtcatname.ValueMember = "CatName"
        Me.txtcatname.DisplayMember = "CatName"
        Me.txtcatname.DataSource = dt
    End Sub

    Private Sub txtcatname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcatname.SelectedIndexChanged
        Dim cmd As New SqlCommand("select CatID from tblcategory where catName=N'" & txtcatname.SelectedValue & "'", cn)
        txtcatid.Text = cmd.ExecuteScalar
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Categoryinfo.ShowDialog()
    End Sub

    Private Sub btna_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        cmd.Connection = cn
        cmd.CommandText = "select procode from tblproduct where procode='" & txtprocode.Text & "'"
        Dim cod = cmd.ExecuteScalar
        If txtprocode.Text = "" Then
            MessageBox.Show("Please Reguire field Product Code", "Field Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            txtprocode.Focus()
            Exit Sub
        ElseIf txtprocode.Text = cod Then
            MessageBox.Show("This Product code already exist", "Field Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Exit Sub
        End If
        If txtproname.Text = "" Then
            MessageBox.Show("Please Reguire field Product name", "Field Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            txtproname.Focus()
            Exit Sub
        End If
        If txtpricein.Text = "" Then
            MessageBox.Show("Please reguire field Price In", "Field Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            txtpricein.Focus()
            Exit Sub
        End If
        If txtpricout.Text = "" Then
            MessageBox.Show("Please reguire field Price out", "Field Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            txtpricout.Focus()
            Exit Sub
        End If
        If txtqty.Text = "" Then
            MessageBox.Show("Please reguire field Quality", "Field Info", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            txtqty.Focus()
            Exit Sub
        End If
        cmd.Connection = cn
        cmd.CommandText = "insert into tblproduct values(@procode,@catid,@proname,@pricein,@priceout,@qty,@buydate,@exp,@description)"
        cmd.Parameters.Add("@procode", SqlDbType.NVarChar).Value = txtprocode.Text
        cmd.Parameters.Add("@catid", SqlDbType.Int).Value = txtcatid.Text
        cmd.Parameters.Add("@proname", SqlDbType.NVarChar).Value = txtproname.Text
        cmd.Parameters.Add("@pricein", SqlDbType.Float).Value = txtpricein.Text
        cmd.Parameters.Add("@priceout", SqlDbType.Float).Value = txtpricout.Text
        cmd.Parameters.Add("@qty", SqlDbType.Int).Value = txtqty.Text
        cmd.Parameters.Add("@buydate", SqlDbType.Date).Value = txtbydate.Value
        cmd.Parameters.Add("@exp", SqlDbType.Date).Value = txtexpiredate.Value
        cmd.Parameters.Add("@description", SqlDbType.NVarChar).Value = txtdescription.Text
        cmd.ExecuteNonQuery()
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        cmd.Connection = cn
        cmd.CommandText = "update Tblproduct Set Procode=@procode,catid=@catid,proName= @ProName,pricein=@Pricein,priceout=@Priceout,qty=@qty,madedate=@madedate,Expiredate=@Expiredate,description=@description where procode=@Procode"
        cmd.Parameters.Add("@procode", SqlDbType.NVarChar).Value = txtprocode.Text
        cmd.Parameters.Add("@catid", SqlDbType.Int).Value = txtcatid.Text
        cmd.Parameters.Add("@proName", SqlDbType.NVarChar).Value = txtproname.Text
        cmd.Parameters.Add("@pricein", SqlDbType.Float).Value = txtpricein.Text
        cmd.Parameters.Add("@priceout", SqlDbType.Float).Value = txtpricout.Text
        cmd.Parameters.Add("@qty", SqlDbType.Int).Value = txtqty.Text
        cmd.Parameters.Add("@madedate", SqlDbType.Date).Value = txtbydate.Value
        cmd.Parameters.Add("@Expiredate", SqlDbType.Date).Value = txtexpiredate.Value
        cmd.Parameters.Add("@description", SqlDbType.NVarChar).Value = txtdescription.Text
        cmd.ExecuteNonQuery()
        MessageBox.Show("Update Succes", "Successfully", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Close()
    End Sub
End Class